import pandas as pd
import numpy as np

def process_nan_value(data):
    '''
    处理data中缺失值，有缺失值的特征为`Age`，`Cabin`，`Embarked`。
    :param data: 训练集的特征，类型为DataFrame
    :return:处理好缺失值后的训练集特征，类型为DataFrame
    '''

    #********* Begin *********#
    data['Initial'] = 0
    for i in data:
        data.loc[:, 'Initial'] = data.Name.str.extract('([A-Za-z]+)\.', expand=False)

    data.loc[:, 'Initial'].replace(
        ['Mlle', 'Mme', 'Ms', 'Dr', 'Major', 'Lady', 'Countess', 'Jonkheer', 'Col', 'Rev', 'Capt', 'Sir', 'Don',
         'Master'],
        ['Miss', 'Miss', 'Miss', 'Other', 'Mr', 'Mrs', 'Mrs', 'Other', 'Other', 'Other', 'Mr', 'Mr', 'Mr', 'Other'],
        inplace=True)

    data.groupby('Initial')['Age'].mean()

    data.loc[(data.Age.isnull()) & (data.Initial == 'Mr'), 'Age'] = 33
    data.loc[(data.Age.isnull()) & (data.Initial == 'Mrs'), 'Age'] = 36
    data.loc[(data.Age.isnull()) & (data.Initial == 'Miss'), 'Age'] = 22
    data.loc[(data.Age.isnull()) & (data.Initial == 'Other'), 'Age'] = 46

    data['Embarked'].fillna('S', inplace=True)

    data.drop(['Cabin'], axis=1, inplace=True)

    return data
    #********* End *********#
